// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;
using SnapObjects.Commons;

namespace SnapObjects.Data
{
    /// <summary>
    ///     Represents a computed column that a property is mapped to.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public class SqlComputeAttribute : Attribute, ISqlCompute
    {
        /// <summary>
        ///     Initializes a new instance of the SqlComputeAttribute class.
        /// </summary>
        /// <param name="expression">A SQL expression which represents the computed column.</param>
        public SqlComputeAttribute(string expression)
        {
            this.Expression = expression;
            this.Order = -1;

            Check.NotEmpty(expression, nameof(expression));
        }

        /// <summary>
        ///     Initializes a new instance of the SqlComputeAttribute class.
        /// </summary>
        /// <param name="expression">A SQL expression which represents the computed column.</param>
        /// <param name="alias">The alias of the computed column.</param>
        public SqlComputeAttribute(string expression, string alias)
        {
            this.Expression = expression;
            this.Alias = alias;
            this.Order = -1;

            Check.NotEmpty(expression, nameof(expression));
            Check.NotEmpty(alias, nameof(alias));
        }

        /// <summary>
        ///     Gets the SQL expression which represents the computed column.
        /// </summary>
        public string Expression { get; }

        /// <summary>
        ///     Gets the alias of the computed column.
        /// </summary>
        public string Alias { get; }

        /// <summary>
        ///     Gets whether the computed column has an alias.
        /// </summary>
        public bool AliasDefined => !String.IsNullOrWhiteSpace(this.Alias);

        /// <summary>
        ///     Gets or sets the zero-based order of the computed column which the property is mapped to.
        /// </summary>
        public int Order { get; set; }

        /// <summary>
        ///     Gets or sets the data type (specific to the database provider) of 
        ///     the computed column which the property is mapped to.
        /// </summary>
        public string TypeName { get; set; }
    }
}
